# -*- coding: utf-8 -*

import pygame  # 需要下载
import sys
import tkinter
from pygame.locals import *
from MyLibrary import *



#----- 全局变量
# 窗口大小
SCREEN_X = 675
SCREEN_Y = 600

GRID_WIDTH = SCREEN_X // 15

IFSUM = 13
IFSUMI = 8

# 游戏名称
GAME_NAME = '保卫太阳花'
# 背景颜色
BGCOLOR = (255, 255, 255)
BRIGHTBLUE = (0, 170, 255)
BLACK = (0, 0, 0)
BGIMG = 'assets/bg.png'
BGEGG = 'assets/egg.png'
FLOWER = 'assets/sunflower.png'
BGMONSTER= 'assets/monster.png'

# 地图
MAPOBJ = [
        ['#', '@', '-', '-', '-', '-', '-', '-',],
        ['.', '@', '-', '-', '-', '-', '-', '-',],
        ['.', '@', '-', '-', '@', '@', '@', '-',],
        ['.', '@', '-', '-', '@', '-', '@', '-',],
        ['.', '@', '-', '-', '@', '-', '@', '-',],
        ['.', '@', '-', '-', '@', '-', '@', '-',],
        ['.', '@', '@', '@', '@', '-', '@', '-',],
        ['.', '@', '@', '-', '-', '-', '@', '-',],
        ['.', '@', '@', '-', '-', '-', '@', '-',],
        ['.', '@', '@', '-', '-', '-', '@', '-',],
        ['.', '@', '@', '-', '-', '-', '@', '-',],
        ['.', '@', '@', '-', '-', '-', '@', '-',],
        ['#', '-', '-', '-', '-', '-', '@', '-',]
    ]
# 装饰
# 石头
DECORATE_ROCK = {(1,4),(2,6),(9,4),(8,8)}
# 树
DECORATE_TREE = {(6,3),(5,8),(10,6),(12,4)}
# 蛋
EGG =(13,7)
# 怪物
MONSTER=(1,2)


# 基本图片
IMAGESDICT = {'tree': pygame.image.load('assets/tree.png'),
              'wood': pygame.image.load('assets/wood.png'),
              'plain': pygame.image.load('assets/plain.png'),
              'rock': pygame.image.load('assets/rock.png'),
              'wall': pygame.image.load('assets/wall.png'),
              'egg': pygame.image.load('assets/egg.png'),
              'grass': pygame.image.load('assets/grass.png'),}

GOODS = {'#': IMAGESDICT['wall'], 
        '-': IMAGESDICT['grass'],
        '$': IMAGESDICT['rock'],
        '@': IMAGESDICT['plain'],
        '.': IMAGESDICT['wood'],
        '%': IMAGESDICT['egg'],
        '/':IMAGESDICT['tree'] }

# 绘制地图
class Block(object):
    def __init__(self):
        pass

    # # 绘制地图
    def draw(self, screen ,img ,rect):
        screen.blit(img, rect)


def calc_velocity(direction , vel=0):
    velocity = Point(0,0)
    if direction == 0: #上
        velocity.y = -vel
    elif direction == 2: #右
        velocity.x = vel
    elif direction == 4: #下
        velocity.y = vel
    elif direction == 6: #左
        velocity.x = -vel
    return velocity

def main():
    # 初始化pygame
    pygame.init()
    screen_size = (SCREEN_X, SCREEN_Y)
    # 显示窗口
    screen = pygame.display.set_mode(screen_size)
    pygame.display.set_caption(GAME_NAME)

    #创建精灵组
    group = pygame.sprite.Group()
    # 游戏时间
    framerate = pygame.time.Clock()
    
    GAME_OVER = True;

    # 地图 -类
    mapBlock = Block()
    # 精灵组-类
    myEgg = MySprite()
    # 怪物-类
    myMonster = MySprite()

    # 蛋
    myEgg.load(FLOWER, 84, 77, 2)
    myEgg.position = GRID_WIDTH *EGG[0]-15, GRID_WIDTH *EGG[1]-27
    group.add(myEgg)

    # 怪物
    myMonster.load(BGMONSTER, 45, 65, 2)
    myMonster.position = GRID_WIDTH *MONSTER[0]-5, GRID_WIDTH *MONSTER[1]-20 
    myMonster.direction = 2
    group.add(myMonster)

    # 死循环确保窗口一直显示
    while True:
        framerate.tick(100)

        for event in pygame.event.get():  # 遍历所有事件
            if event.type == pygame.QUIT:  # 如果单击关闭窗口，则退出
                sys.exit()


        ticks = pygame.time.get_ticks()

        # 背景颜色
        screen.fill(BRIGHTBLUE)
        # 背景图片
        background = pygame.image.load(BGIMG).convert()
        screen.blit(background,(25,25))

        # 背景
        for i in range(IFSUM):
            for j in range(IFSUMI):
                mapBlock.draw(screen,GOODS[MAPOBJ[i][j]],(GRID_WIDTH *(1 + i)-5, GRID_WIDTH *(1 + j)-20))
        # 装饰 -石头
        for i in DECORATE_ROCK:
            mapBlock.draw(screen,GOODS['$'],(GRID_WIDTH *i[0]-5, GRID_WIDTH *i[1]-20))
        # 装饰 -树
        for i in DECORATE_TREE:
            mapBlock.draw(screen,GOODS['/'],(GRID_WIDTH *i[0]-5, GRID_WIDTH *i[1]-20))

        # 怪物移动
        if GAME_OVER:
            myMonster.velocity = calc_velocity(myMonster.direction,1)
            myMonster.velocity.x  *= 1.5
            myMonster.velocity.y  *= 1.5

            myMonster.X += myMonster.velocity.x
            myMonster.Y += myMonster.velocity.y
            print(myMonster.X)
            print(myMonster.Y)

        if myMonster.X == 540 and myMonster.Y == 70:
            myMonster.direction = 4 #下
        elif myMonster.X == 540 and myMonster.Y == 110:
            myMonster.direction = 6 #左
        elif myMonster.X == 314 and myMonster.Y == 110:
            myMonster.direction = 4 #下
        elif myMonster.X == 314 and myMonster.Y == 204:
            myMonster.direction = 6 #左
        elif myMonster.X == 126 and myMonster.Y == 204:
            myMonster.direction = 4 #下
        elif myMonster.X == 126 and myMonster.Y == 294:
            myMonster.direction = 2 #右
        elif myMonster.X == 573 and myMonster.Y == 294:
            GAME_OVER = False


        group.update(ticks,200)
        group.draw(screen)

        pygame.display.flip()

if __name__ == '__main__':
    main()
